JSGF.Controller = JSGF.inherit(JSGF.Object);

JSGF.Controller.prototype.init = function(element) {
	this.type = "JSGF.Controller";
	this.canvas = null;
	this.stage = null;
	this.localStrageName = "";
	if (element == null) {
		this.canvas = document.createElement("canvas");
		document.body.appendChild(this.canvas);
	} else {
		if (element.tagName && element.tagName.toLowerCase() == "canvas") {
			this.canvas = element;
		} else {
			return null;
		}
	}
	if (this.canvas == null || !this.canvas.id) {
		this.canvas.id = "canvas";
	}

	if (!this.canvas.getContext) {
		this.onNoCanvas();
		return null;
	}

	this.context = this.canvas.getContext("2d");
	this.onLoad();
}

JSGF.Controller.prototype.release = function() {
	this.onUnload();
	this.context.clearRect( 0, 0, this.canvas.width, this.canvas.height );
	document.body.removeChild(this.canvas);
}

JSGF.Controller.prototype.getDataString = function(data) {
	var str = "";
	var first = true;
	str += "{";
	for(var key in data) {
		var value = data[key];
		var type_key = typeof(key);
		var type_value = typeof(value);
		if (type_key == "string" || type_key == "number") {
			if (type_value == "string" || type_value == "number" || type_value == "object") {
				if (first == true) {
					first = false;
				} else {
					str += ",";
				}
				str += key + ":";
				if (type_value == "object") {
					str += this.getDataString(value);
				} else {
					str += value;
				}
			}
		}
	}
	str += "}";
	return str;
}

////////////////////////////////////////
// Public
////////////////////////////////////////

JSGF.Controller.prototype.setWidth = function(width) {
	this.canvas.width = width;
}

JSGF.Controller.prototype.setHeight = function(height) {
	this.canvas.height = height;
}

JSGF.Controller.prototype.setStyle = function(name, style) {
	if (typeof(name) == "string") {
		this.canvas.style[name] = style;
	}
}

JSGF.Controller.prototype.getWidth = function() {
	return this.canvas.width;
}

JSGF.Controller.prototype.getHeight = function() {
	return this.canvas.height;
}

JSGF.Controller.prototype.getStyle = function(name) {
	if (typeof(name) == "string") {
		return this.canvas.style[name];
	}
}

JSGF.Controller.prototype.getCanvas = function() {
	return this.stage;
}

JSGF.Controller.prototype.getStage = function() {
	return this.stage;
}

JSGF.Controller.prototype.loadStage = function(name) {
	if (typeof(name) != "string") {
		return false;
	}
	this.unloadStage();
	eval("var stage = new " + name + "(this);");
	if (!stage.type || stage.type !="JSGF.Stage") {
		return false;
	}
	this.stage = stage;
	return true;
}

JSGF.Controller.prototype.unloadStage = function() {
	if (this.stage != null) {
		this.stage.release();
		this.stage = null;
	}
}

JSGF.Controller.prototype.useLocalStrage = function(name) {
	if (name == null || name == "") {
		return false;
	}
	this.localStrageName = name;
	return true;
}

JSGF.Controller.prototype.clearLocalStrage = function() {
	if (this.localStrageName == null || this.localStrageName == "") {
		return false;
	}
	localStorage.removeItem[this.localStrageName];
}

JSGF.Controller.prototype.setLocalStrage = function(key, value) {
	if (this.localStrageName == null || this.localStrageName == "") {
		return false;
	}
	var data = this.setLocalStrage[this.localStrageName];
	if (data == null) {
		data = new Object();
	}
	var type_key = typeof(key);
	var type_value = typeof(value);
	if (type_key == "string" || type_key == "number") {
		if (type_value == "string" || type_value == "number" || type_value == "object") {
			data[key] = value;
			var str = this.getDataString(data);
			localStorage.setItem(this.localStrageName , str);
			return true;
		}
	}
	return false;
}

JSGF.Controller.prototype.getLocalStrage = function(key) {
	if (this.localStrageName == null || this.localStrageName == "") {
		return null;
	}
	var str = localStorage.getItem(this.localStrageName);
	if (str == null) {
		return null;
	}
	eval("var data = " + str);
	return data[key];
}

////////////////////////////////////////
// definition by user
////////////////////////////////////////

JSGF.Controller.prototype.onLoad = function() {

}

JSGF.Controller.prototype.onUnload = function() {

}

JSGF.Controller.prototype.onNoCanvas = function() {

}
